package appModel;

import gridModel.GridSize;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.List;
import java.util.Set;

import appModel.GridsCollection.Categories;

public class StdGalleryModel implements GalleryModel {
	
	// ATTRIBUTS
	
	private PropertyChangeSupport listeners;
	
	private final Categories category;
	private GridSize gridSize;
	private final GridsCollection collection = GridsCollection.getGridsCollection(this);
	
	// CONSTRUCTEUR
	
	public StdGalleryModel(Categories c) {
		if (c == null) {
			throw new IllegalArgumentException();
		}
		category = c;
		gridSize = null;
	}
	
	// REQUETES

	@Override
	public Categories getCategory() {
		return category;
	}

	@Override
	public GridSize getGridSize() {
		return gridSize;
	}
	
	@Override
	public Set<GridSize> getGridSizes() {
		return collection.getGridsGroup(category).keySet();
	}
	
	@Override
	public List<String> getFiles() {
		return collection.getGrids(category, gridSize);
	}

	@Override
	public PropertyChangeListener[] getPropertyChangeListeners() {
		if (listeners == null) {
			return null;
		}
		return listeners.getPropertyChangeListeners();
	}
	
	// COMMANDES
	
	@Override
	public void activeGridSize(GridSize s) {
		GridSize oldValue = gridSize;
		gridSize = s;
		if (listeners != null) {
			listeners.firePropertyChange("gridSize", oldValue, gridSize);
		}
	}

	@Override
	public void addPropertyChangeListener(PropertyChangeListener l) {
		if (l == null) {
			throw new IllegalArgumentException();
		}
		if (listeners == null) {
			listeners = new PropertyChangeSupport(this);
		}
		listeners.addPropertyChangeListener(l);
	}

	@Override
	public void removePropertyChangeListener(PropertyChangeListener l) {
		if (l == null) {
			throw new IllegalArgumentException();
		}
		listeners.removePropertyChangeListener(l);
	}

}
